package service.impl.v2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.AdVOMapper;
import model.AdVO;
import service.v2.AdService;
@Service
public class AdServiceImpl implements AdService {
	@Autowired
	private AdVOMapper adVOMapper;
	
	/**查询广告列表*/
	public List<AdVO> selectAdVoAll(AdVO adVO, String os, String member_id, String vip_level) {
		List<AdVO> adVoList = adVOMapper.selectAdVoAll(adVO, os, member_id, vip_level);
		
		AdVO vo = null;
		for(int i=0;i<adVoList.size();i++){
			vo = new AdVO();
			
			if("2".equals(adVoList.get(i).getType())){
				String content = adVoList.get(i).getUrl_content();
				adVoList.get(i).setShare(vo);
				adVoList.get(i).getShare().setImage(adVoList.get(i).getImg()+"?x-oss-process=image/resize,h_100");
				adVoList.get(i).getShare().setUrl(adVoList.get(i).getUrl());
				adVoList.get(i).getShare().setTitle(adVoList.get(i).getTitle());
				if(content !=null){
					Document doc = Jsoup.parse(content, "UTF-8");
					if(!"".equals(doc.text())){
						adVoList.get(i).getShare().setContent(doc.text().substring(doc.text().indexOf(adVoList.get(i).getTitle())+adVoList.get(i).getTitle().length()));
					}else{
						adVoList.get(i).getShare().setContent("");
					}
				}else{
					adVoList.get(i).getShare().setContent("");
				}
				adVoList.get(i).getShare().setGood_id(null);
				adVoList.get(i).getShare().setList_id(null);
				adVoList.get(i).getShare().setCat_id(null);
			}else{
				adVoList.get(i).setShare(vo);
			}
		}

		return adVoList;
	}

	public void processImg(List<AdVO> adList, String screenwidth, int i) {
		for (AdVO vo : adList) {
			if(!"".equals(vo.getImg())){
				vo.setImg(vo.getImg()+"?x-oss-process=image/resize,w_"+Integer.parseInt(screenwidth)/i);
			}
		}
	}

	public AdVO getContentById(String ad_id) {
		return adVOMapper.getContentById( ad_id);
	}
	
	public int getMaterialRead(String member_id) {
		return adVOMapper.getMaterialRead(member_id);
	}
	public void setRead(String member_id) {
		int num = adVOMapper.getMaterialRead(member_id);
		if(num==0){
			adVOMapper.setRead(member_id);
		}
	}

	public List<Map<String, List<AdVO>>> processRow(List<AdVO> adList) {
		Map<String, List<AdVO>> map= new LinkedHashMap<String, List<AdVO>>();
		for (AdVO adVO : adList) {
			List<AdVO> l = map.get(adVO.getRow());
			if(l == null){
				l = new ArrayList<AdVO>();
			}
			l.add(adVO);
			map.put(adVO.getRow(), l);
		}
		
		List<Map<String, List<AdVO>>> list = new ArrayList<Map<String, List<AdVO>>>();
		for (String row : map.keySet()) {
			Map m = new HashMap();
			m.put("row", row);
			m.put("list", map.get(row));
			list.add(m);
		}
		
		return list;
	}
	
}
